<template>
	<view class="container">
		<!-- 搜索框 -->
		<view class="search-box">
			<input type="text" placeholder="搜索附近的宠物商店" @input="onSearch" />
		</view>

		<!-- 地图 -->
		<map id="map" :latitude="location.latitude" :longitude="location.longitude" :markers="markers"
			@markertap="onMarkerTap" class="map" show-location></map>

		<!-- 店铺信息 -->
		<view class="shop-info" v-if="selectedShop">
			<view class="shop-info-top">
				<image :src="loadUrl + selectedShop.image" class="shop-image" />
				<view class="shop-summary">
					<view class="shop-name">{{ selectedShop.name }}</view>
					<view class="shop-rating">
						<span>
							<uni-icons v-for="item in selectedShop.rating" type="star-filled" size="30" color="#fdd300"></uni-icons>
							<uni-icons v-for="item in (5 - selectedShop.rating)" type="star-filled" size="30"
								color="#c1c0c9"></uni-icons>
						</span>
						<span style="margin-left: 10px;">{{ selectedShop.commentCount }} 评论</span>
					</view>
				</view>
			</view>
			<view class="divider"></view>
			<view class="shop-details">
				<view class="shop-address">{{ selectedShop.address }}</view>
				<view class="shop-contact">联系方式: {{ selectedShop.contact }}</view>
				<view class="shop-hours">营业时间: {{ selectedShop.hours }}</view>
			</view>
			<view>
				<button class="navigate-button" @click="navigateToShop">到这儿去</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				markers: [],
				shops: [], // 添加此行用于存储所有店铺信息
				selectedShop: null,
				loadUrl: this.$loadUrl
			}
		},
		computed: {
			...mapState('location', ['location']),
		},
		created() {
			this.fetchShops();
		},
		methods: {
			fetchShops() {
				this.$service({
					url: '/stores/nearby',
					method: 'GET'
				}).then(res => {
					if (res.code == 0) {
						this.shops = res.data;
						this.markers = res.data.map(shop => ({
							id: shop.id,
							latitude: shop.latitude,
							longitude: shop.longitude,
							iconPath: '/static/images/chongwudian.png',
							width: 30,
							height: 30,
							callout: {
								content: shop.name,
								color: '#000',
								fontSize: 12,
								borderRadius: 5,
								bgColor: '#fff',
								padding: 5,
								display: 'ALWAYS'
							}
						}));
					}
				})
			},
			onSearch(event) {
				// 处理搜索逻辑
				console.log('搜索关键词:', event.target.value);
			},
			onMarkerTap(e) {
				const markerId = e.detail.markerId;
				this.selectedShop = this.shops.find(shop => shop.id === markerId); // 查找店铺详细信息
			},
			navigateToShop() {
				uni.openLocation({
					latitude: this.selectedShop.latitude,
					longitude: this.selectedShop.longitude,
					name: this.selectedShop.name,
					address: this.selectedShop.address
				});
			}
		}
	};
</script>

<style scoped>
	.container {
		display: flex;
		flex-direction: column;
		height: 100vh;
	}

	.search-box {
		padding: 10px;
		background-color: #fff;
	}

	.map {
		flex: 1;
		width: 100%;
	}

	.shop-info {
		padding: 20px;
		background-color: #fff;
	}

	.shop-info-top {
		display: flex;
		align-items: center;
	}

	.shop-image {
		width: 100px;
		/* 调整图片宽度 */
		height: 100px;
		/* 调整图片高度 */
		object-fit: cover;
		margin-right: 10px;
		/* 调整图片和文字之间的间距 */
	}

	.shop-summary {
		display: flex;
		flex-direction: column;
	}

	.shop-name {
		font-size: 20px;
		font-weight: bold;
	}

	.shop-rating {
		display: flex;
		align-items: center;
		color: #c1c0c9;
	}

	.star-icon {
		margin-right: 2px;
	}

	.divider {
		height: 1px;
		background-color: #e0e0e0;
		margin: 10px 0;
	}

	.shop-details {
		padding: 6px 2px;
	}

	.shop-address,
	.shop-contact,
	.shop-hours {
		color: #666;
		padding: 2px;
	}

	.navigate-button {
		margin-top: 10px;
		background-color: #fdd300;
		color: #fff;
		text-align: center;
		border: none;
		border-radius: 5px;
	}
</style>